package com.chesnutconsulting.reports;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ReportRequestManager {

	@Autowired
    private MailSender mailSender;

	@Autowired
	private SimpleMailMessage mailMessage;

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

	@RequestMapping(value = "/request-report", method = RequestMethod.GET)
    public ModelAndView requestReport(@ModelAttribute("reportRequest") ReportRequest reportRequest,
    		BindingResult result		
    ) {

    	System.out.println("----->" + reportRequest.getReportRequestCategory());
    	System.out.println("----->" + reportRequest.getReportRequestDescription());
    	
    	mailMessage.setText("Report requested with category [" +  reportRequest.getReportRequestCategory() + "] and description[" +  reportRequest.getReportRequestDescription() + "]");
    	
        try{
            this.mailSender.send(mailMessage);
        }
        catch(MailException ex) {
            // simply log it and go on...
            System.err.println(ex.getMessage());            
        }
    	
		ModelAndView model = new ModelAndView("reportSelection");
		model.addObject(ReportsInitializer.getReportWidgetTypeMap());
		model.addObject("uiConfig", ReportsInitializer.getReportWidgetTypeMap());
		return model;
		
    }
}